/**
 * 生成与解析jwtToken信息
 * @param 可传入多个对象
 * @return token与解析
 */

const JsonWebToken = require('jsonwebtoken');
const config = require('../config');
const logging = require('./log');

/**
 * 生成token
 * @param userId
 * @param name
 * @param role
 * @param time
 * @return {boolean}
 * @constructor
 */
const JsonToken = (userId, name, role, time) => {
    const token = JsonWebToken.sign({
        userId: userId,
        name: name,
        role: role,
        time: time,
    }, config().jwt.secretKey, {expiresIn: config().jwt.TimeOut})
    return token;
}

/**
 *
 * @param Jwt
 * @return {*|boolean}
 * @constructor
 */
const JwtDecoded = (Jwt) => {
    try {
        return JsonWebToken.verify(Jwt, config().jwt.secretKey)
    } catch (err) {
        logging('error',err)
        return false;
    }
}

module.exports = {JsonToken, JwtDecoded}
